/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.modules.activity.service.impl;

import cn.hutool.core.util.StrUtil;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.activity.domain.YxStoreCoupon;
import co.yixiang.modules.activity.domain.YxStoreCouponRange;
import co.yixiang.modules.activity.service.YxStoreCouponRangeService;
import co.yixiang.modules.activity.service.YxStoreCouponService;
import co.yixiang.modules.activity.service.dto.YxStoreCouponDto;
import co.yixiang.modules.activity.service.dto.YxStoreCouponQueryCriteria;
import co.yixiang.modules.activity.service.mapper.YxStoreCouponMapper;
import co.yixiang.modules.activity.service.mapper.YxStoreCouponRangeMapper;
import co.yixiang.modules.product.domain.YxStoreProduct;
import co.yixiang.modules.product.service.YxStoreProductService;
import co.yixiang.modules.user.domain.YxUser;
import co.yixiang.utils.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;


/**
* @author hupeng
* @date 2020-05-13
*/
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreCouponRangeServiceImpl extends BaseServiceImpl<YxStoreCouponRangeMapper, YxStoreCouponRange> implements YxStoreCouponRangeService {


    @Override
    public boolean saveOrUpdate(Integer cid,List<YxUser> userList) {
        List<YxStoreCouponRange> yxStoreCouponRanges = this.baseMapper.selectList(Wrappers.<YxStoreCouponRange>lambdaQuery()
                .eq(YxStoreCouponRange::getCouponId, cid));

        //删除多余
        for (YxStoreCouponRange yxStoreCouponRange : yxStoreCouponRanges) {
            Long uid = yxStoreCouponRange.getUid();
            long count = userList.stream().filter(e -> e.getUid().equals(uid)).count();
            if(count==0){
                this.baseMapper.delete(Wrappers.<YxStoreCouponRange>lambdaQuery()
                        .eq(YxStoreCouponRange::getCouponId,cid)
                        .eq(YxStoreCouponRange::getUid,uid)
                );
            }
        }
        for (YxUser yxUser : userList) {
            long count = yxStoreCouponRanges.stream().filter(e -> e.getUid().equals(yxUser.getUid())).count();
            if(count==0){
                this.baseMapper.insert(YxStoreCouponRange.builder()
                        .uid(yxUser.getUid())
                        .couponId(cid)
                        .build());
            }
        }

        return true;
    }

    @Override
    public List<YxUser> findYxUserByCid(Integer cid) {

        return this.baseMapper.findYxUserByCid(cid);
    }
}
